package com.starbaba.weixin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.weixin.model.Weixin_coupon;
import com.starbaba.weixin.service.WeixinCouponService;

@Component
public class WeixinCouponServcieImpl implements WeixinCouponService {

	BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	@Override
	public List<Weixin_coupon> getWCResult() {
		String sql = "select * from weixin_activity_coupon ";
		List<Object> params = new ArrayList<Object>();
		List<Weixin_coupon> list = null;
		try {
			list = dao.list(sql, Weixin_coupon.class,params);
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}

	@Override
	public List<Weixin_coupon> getDataList(int iDisplayStart, int iDisplayLength, Weixin_coupon model, PageResponse pageResponse, CommonService service) throws AppException {
		String sql = "select * from weixin_activity_coupon where 1 = 1 ";
		List<Object> params= new ArrayList<Object>();
		if(null != model){
			if(!StringUtils.isEmpty(model.getChannel())){
				sql = sql + " and channel = ? ";
				params.add(model.getChannel());
			}
			if(!StringUtils.isEmpty(model.getKeyword())){
				sql = sql + " and keyword = ? ";
				params.add(model.getKeyword());
			}
			if(null != model.getCoupon_type()){
				sql = sql +  " and coupon_type = ? ";
				params.add(model.getCoupon_type());
			}
		}
		sql = sql + " limit " + iDisplayStart + "," + iDisplayLength;
		List<Weixin_coupon> result = dao.list(sql, Weixin_coupon.class,params);
		return result;
	}

	@Override
	public List<Weixin_coupon> getById(int iDisplayStart, int iDisplayLength, Weixin_coupon model, PageResponse pageResponse, String id) throws AppException {
		String sql = "select * from weixin_activity_coupon where 1=1 ";
		List<Object> params= new ArrayList<Object>();
		if(null != model){
			if(null != model.getId()){
				sql = sql + " and id = ? ";
				params.add(model.getId());
			}
			
			if(!StringUtils.isEmpty(model.getChannel())){
				sql = sql + " and channel = ? ";
				params.add(model.getChannel());
			}
			if(!StringUtils.isEmpty(model.getKeyword())){
				sql = sql + " and keyword = ? ";
				params.add(model.getKeyword());
			}
			if(null != model.getCoupon_type()){
				sql = sql +  " and coupon_type = ? ";
				params.add(model.getCoupon_type());
			}
		}
		sql = sql + " limit " + iDisplayStart + "," + iDisplayLength;
		List<Weixin_coupon> result = dao.list(sql, Weixin_coupon.class,params);
		return result;
	}

}
